package cn.south.campus.admin.sys.service.impl;

import cn.south.campus.admin.sys.mapper.SysDeptMapper;
import cn.south.campus.admin.sys.model.entity.SysDept;
import cn.south.campus.admin.sys.model.entity.SysUser;
import cn.south.campus.admin.sys.model.enums.EnableEnums;
import cn.south.campus.admin.sys.model.enums.SysRespEnums;
import cn.south.campus.admin.sys.model.req.SysDeptReq;
import cn.south.campus.admin.sys.model.vo.SysDeptVo;
import cn.south.campus.admin.sys.service.SysDeptService;
import cn.south.campus.admin.sys.service.SysUserService;
import cn.south.campus.common.model.enums.CommonEnums;
import cn.south.campus.common.utils.AssertUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * SysDept 实现层
 *
 * @author chow
 * @since 2024-11-04
 **/
@Service
@AllArgsConstructor
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    private final SysDeptMapper sysDeptMapper;

    private final SysUserService sysUserService;

    @Override
    public void add(SysDeptVo vo) {
        SysDept sysDept = SysDeptVo.convert(vo);
        sysDept.setStatus(EnableEnums.ENABLE.getCode());
        sysDeptMapper.insert(sysDept);
    }

    @Override
    public void delete(Long id) {
        //查询该部门下是否有子部门
        Long count = sysDeptMapper.selectCount(new LambdaQueryWrapper<SysDept>().eq(SysDept::getParentId, id));
        AssertUtils.isLtE(count, 1L, SysRespEnums.DEPT_HAS_CHILD_ERROR);
        //检测该部门下是否存在用户
        List<SysUser> sysUser = sysUserService.queryByDeptId(id);
        AssertUtils.isNotBlankCollection(sysUser, SysRespEnums.DEPT_HAS_USER_ERROR);
        sysDeptMapper.deleteById(id);
    }

    @Override
    public void update(SysDeptVo vo) {
        SysDept sysDept = getById(vo.getId());
        AssertUtils.isNotNull(sysDept, CommonEnums.DATA_HAS_NOT_ERROR);
        sysDept.setParentId(vo.getParentId());
        sysDept.setName(vo.getName());
        sysDept.setSort(vo.getSort());
        sysDept.setLeader(vo.getLeader());
        sysDept.setMobile(vo.getMobile());
        sysDept.setEmail(vo.getEmail());
        sysDept.setStatus(vo.getStatus());
        sysDeptMapper.updateById(sysDept);
    }

    @Override
    public List<SysDeptVo> getList(SysDeptReq req) {
        LambdaQueryWrapper<SysDept> wrapper = new LambdaQueryWrapper<SysDept>()
                .eq(req.getId() != null, SysDept::getId, req.getId())
                .like(StringUtils.isNotEmpty(req.getDeptName()), SysDept::getName, req.getDeptName())
                .orderByDesc(SysDept::getSort);
        List<SysDept> sysDeptList = sysDeptMapper.selectList(wrapper);
        return buildDeptList(sysDeptList, 0L, req.getId(), req.getDeptName());
    }

    /**
     * 构建部门树
     *
     * @param sysDeptList 部门列表
     * @param parentId 父级ID
     * @return 部门树
     */
    private List<SysDeptVo> buildDeptList(List<SysDept> sysDeptList, Long parentId, Long id, String deptName) {
        if (Objects.nonNull(id) || StringUtils.isNotEmpty(deptName)) {
            for (SysDept sysDept : sysDeptList) {
                if (sysDept.getParentId().equals(parentId)) {
                    return sysDeptList.stream().map(SysDeptVo::convert).collect(Collectors.toList());
                }
            }
            //搜索
            return sysDeptList.stream().filter(sysDept -> !sysDept.getParentId().equals(parentId)).
                    map(SysDeptVo::convert).collect(Collectors.toList());
        }
        //构建父级部门
        ArrayList<SysDeptVo> list = sysDeptList.stream().
                filter(sysDept -> sysDept.getParentId().equals(parentId)).
                map(SysDeptVo::convert).collect(Collectors.toCollection(ArrayList::new));
        //遍历所有父部门
        for (SysDeptVo sysDeptListVo : list) {
            //查询该父级部门下的子部门
            List<SysDeptVo> childrenDepts = buildDeptList(sysDeptList, sysDeptListVo.getId(), id, deptName);
            if (childrenDepts.size() > 0) {
                //将子部门添加父部门
                sysDeptListVo.setChildren(childrenDepts);
            }
        }
        return list;
    }
}
